package org.jc.oss.service.impl;

import jakarta.annotation.Resource;
import org.jc.oss.entity.FileInfo;
import org.jc.oss.service.StorageService;
import org.jc.oss.util.MinioUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * 功能描述
 *
 * @author wanghongqian
 * @date 2024/07/04 15:42
 */
@Service
public class MinioStorageServiceImpl implements StorageService {

    @Resource
    private MinioUtil minioUtil;

    @Override
    public void createBucket(String bucket) throws Exception {
        minioUtil.createBucket(bucket);
    }

    @Override
    public void uploadFile(MultipartFile uploadFile, String bucket, String objectName) throws Exception{
        minioUtil.createBucket(bucket);
        if(objectName != null){
            minioUtil.uploadFile(uploadFile.getInputStream(),bucket,objectName+"/"+uploadFile.getName());
        }else{
            minioUtil.uploadFile(uploadFile.getInputStream(),bucket, uploadFile.getName());
        }
    }

    @Override
    public List<String> getAllBucket() throws Exception {
        return minioUtil.getAllBucket();
    }

    @Override
    public List<FileInfo> getAllFile(String bucket) throws Exception {
        return minioUtil.getAllFile(bucket);
    }

    @Override
    public InputStream download(String bucket, String objectName) throws Exception {
        return minioUtil.download(bucket,objectName);
    }

    @Override
    public void deleteBucket(String bucket) throws Exception {
        minioUtil.deleteBucket(bucket);
    }
}
